package model

import (
	"time"

	"bmember/pkg/util"
)

// MemberAddress 用户收货地址
type MemberAddress struct {
	// id
	Id int64 `json:"id" sql:"id"`
	// 品牌id
	BrandId int64 `json:"brand_id" sql:"brand_id"`
	// 用户id
	MemberId int64 `json:"member_id" sql:"member_id"`
	// 联系人
	ContactName string `json:"contact_name" sql:"contact_name"`
	// 联系手机号
	ContactMobile string `json:"contact_mobile" sql:"contact_mobile"`
	// 性别 0~未知，1~女，2~男
	Gender int8 `json:"gender" sql:"gender"`
	// 地址缩写
	Title string `json:"title" sql:"title"`
	// 地址
	Address string `json:"address" sql:"address"`
	// 楼号，门牌号号
	StreetNo string `json:"street_no" sql:"street_no"`
	// 行政区域编码
	CityCode string `json:"city_code" sql:"city_code"`
	// 标签
	Tag string `json:"tag" sql:"tag"`
	// 纬度
	Latitude float64 `json:"latitude" sql:"latitude"`
	// 经度
	Longitude float64   `json:"longitude" sql:"longitude"`
	CreatedAt time.Time `json:"created_at" sql:"created_at"`
	UpdatedAt time.Time `json:"updated_at" sql:"updated_at"`
	DeletedAt int64     `json:"deleted_at" sql:"deleted_at"`
}

// NewMemberAddress 新建地址
func NewMemberAddress(brandId, memberId int64, contactName, contactMobile, tag string, gender int8) *MemberAddress {
	now := util.GetNow()
	return &MemberAddress{
		BrandId:       brandId,
		MemberId:      memberId,
		Gender:        gender,
		ContactName:   contactName,
		ContactMobile: contactMobile,
		Tag:           tag,
		CreatedAt:     now,
		UpdatedAt:     now,
	}
}

// UpdateInfo 更新收货人信息
func (addr *MemberAddress) UpdateInfo(contactName, contactMobile, tag string, gender int8) {
	if contactName != "" {
		addr.ContactName = contactName
	}
	if contactMobile != "" {
		addr.ContactMobile = contactMobile
	}
	if tag != "" {
		addr.Tag = tag
	}
	if gender != 0 {
		addr.Gender = gender
	}
	addr.UpdatedAt = util.GetNow()
}

// SetAddress 设置地址
func (addr *MemberAddress) SetAddress(address, streetNo, cityCode string, lat, lng float64) {
	if address != "" {
		addr.Address = address
	}
	if streetNo != "" {
		addr.StreetNo = streetNo
	}
	if cityCode != "" {
		addr.CityCode = cityCode
	}
	if lat != 0 {
		addr.Latitude = lat
	}
	if lng != 0 {
		addr.Longitude = lng
	}
	addr.UpdatedAt = util.GetNow()
}

// GetGender 获取性别
func (addr *MemberAddress) GetGender() string {
	switch addr.Gender {
	case MemberFemale:
		return MemberFemaleStr
	case MemberMale:
		return MemberMaleStr
	default:
		return ""
	}
}

// Delete 删除
func (addr *MemberAddress) Delete() {
	addr.DeletedAt = util.GetCurrentTimestamp()
}
